#include "client.h"

int main(int argc, const char *argv[])
{
	int sockfd = Bind_client_info(sockfd);
	int is_login;
LOGIN:	
	is_login = Do_login(sockfd);
	if(is_login == 1){
	}else if(is_login == 2){
	}else{
		char ch;
		printf("login failed!\n");
		printf("do you want to try again? (y/n) ");
		scanf("%c", &ch);
		if(ch == 'y' || ch == 'Y'){
			goto LOGIN;
		}else if(ch == 'n' || ch == 'N'){
			//exit
		}
	}
	return 0;
}
int Bind_client_info(int sockfd){

	struct sockaddr_in serveraddr;

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
		ERRLOG("socket failed!");
	}

	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = inet_addr(ADDR);
	serveraddr.sin_port = htons(PORT);

	connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));

	return sockfd;
}

//login complite
int Do_login(int sockfd){
	MSG msg, msgnew;
	int su, num;
	
	printf("***********login***************\n");
	printf("* please enter your name : ");
	scanf("%s", msg.name);
	printf("* please enter your password : ");
	scanf("%s", msg.pwd);
	msg.type = LOAD;
	
	if((num = send(sockfd, &msg, sizeof(MSG), 0)) < 0){
		ERRLOG("send failed!");
	}
	printf("**************%d\n", num);

	recv(sockfd, &msgnew, sizeof(MSG), 0);
	printf("recv  ----- %s\n", msgnew.name);
	if(msgnew.sign == SUCCESS){
		printf("login success!\n");
		Do_after_login(sockfd, &msgnew);
		return 1;
	}else if(msgnew.sign == 999){
		printf("admin login!\n");

		Do_admin_login(sockfd);
		return 2;
	}else{
		return 0;
	}
}

void Do_after_login(int sockfd, MSG *msg){
	int choose, flag = 2;
	printf("*********************************************\n");
	printf("* what do you want to do?   *****************\n");
	printf("* 1.search info  * 2.modify info  ***********\n");
	printf("*********************************************\n");
	printf("* input 1 or 2 to choose (input other to quit) ");
	scanf("%d", &choose);

	if(choose == 1){
		Do_selectuser(sockfd, msg, flag);
	}else if(choose == 2){
		Do_modifyuser(sockfd, msg, flag);
	}else{
		//
	}
}

void Do_admin_login(int sockfd){
	int choose, flag = 1;
	MSG msg;
	printf("**********************************************\n");
	printf("* welcome admin, what do you want to do?  ****\n");
	printf("* 1.add user          * 2.search user    *****\n");
	printf("* 3.modify user info  * 4.delete user    *****\n");
	printf("**********************************************\n");
	printf("* input number to choose (input other to quit)\n ");
	scanf("%d", &choose);

	printf("choose --- %d\n", choose);

	switch(choose){
		case 1:
			Do_adduser(sockfd);
			break;
		case 2:
			printf("***********************\n");
			Do_selectuser(sockfd, &msg, flag);
			break;
		case 3:
			Do_modifyuser(sockfd, &msg, flag);
			break;
		case 4:
			Do_deluser(sockfd);
			break;
		default:
			exit(1);
			break;
	}
}

int Do_adduser(int sockfd){
	INFO info;
	MSG msg;
	int su;
	
	printf("input name : ");
	scanf("%s", info.name);
	printf("input address : ");
	scanf("%s", info.addr);
	printf("input age :");
	scanf("%d", &(info.age));
	printf("input level : ");
	scanf("%d", &(info.level));
	printf("input no : ");
	scanf("%d", &(info.no));
	printf("input phone : ");
	scanf("%s", info.phone);
	printf("input type : ");
	scanf("%d", &(info.type));
	printf("input salary : ");
	scanf("%lf", &(info.salary));

	msg.type = ADD;
	strcpy(msg.name, info.name);
	msg.info = info;

	send(sockfd, &msg, sizeof(MSG), 0);

	recv(sockfd, &su, sizeof(int), 0);

	if(su == 0){
		printf("this user is existed!\n");
	}else if(su == 1){
		printf("add user success!\n");
	}

}

int Do_deluser(int sockfd){
	MSG msg;
	printf("who you want to delete? ");
	scanf("%s", msg.name);
	msg.type = DELETE;
	send(sockfd, &msg, sizeof(msg), 0);

	recv(sockfd, &msg, sizeof(msg), 0);

	printf("%s\n", msg.data);
}

int Do_modifyuser(int sockfd, MSG *msg, int flag){
	
	if(flag == 1){
		//admin
		Do_admin_modify(sockfd);

	}else if(flag == 2){
		//user
		Do_user_modify(sockfd, msg);
	}else{
		//error
	}
}

void Do_admin_modify(int sockfd){
	MSG msgnew;
	INFO info;
	msgnew.type = CHANGE;
	printf("who you want to modify? ");
	scanf("%s", msgnew.name);

	printf("input name : ");
	scanf("%s", info.name);
	printf("input address : ");
	scanf("%s", info.addr);
	printf("input age :");
	scanf("%d", &(info.age));
	printf("input level : ");
	scanf("%d", &(info.level));
	printf("input no : ");
	scanf("%d", &(info.no));
	printf("input phone : ");
	scanf("%s", info.phone);
	printf("input type : ");
	scanf("%d", &(info.type));
	printf("input salary : ");
	scanf("%lf", &(info.salary));

	msgnew.info = info;
	
	send(sockfd, &msgnew, sizeof(MSG), 0);

	recv(sockfd, &msgnew, sizeof(MSG), 0);
	printf("%s\n", msgnew.data);
}

void Do_user_modify(int sockfd, MSG *msg){
	MSG msgnew;
	msgnew.type = USERM;
	strcpy(msgnew.name, msg->name);
	printf("please input your new password : ");
	scanf("%s", msgnew.data);
	send(sockfd, &msgnew, sizeof(MSG), 0);

	recv(sockfd, &msgnew, sizeof(MSG), 0);

	printf("%s\n", msgnew.data);

}

int Do_selectuser(int sockfd, MSG *msg, int flag){
	MSG msgnew;
	msgnew.type = READ;
	if(flag == 1){
		//admin
		printf("who you want to find? ");
		scanf("%s", msgnew.name);
	}else if(flag == 2){
		//user
		strcpy(msgnew.name, msg->name);
	}else{
		//error
	}

	printf("----%s\n", msgnew.name);

	send(sockfd, &msgnew, sizeof(MSG), 0);
	
	recv(sockfd, &msgnew, sizeof(MSG), 0);
    
	if(msgnew.sign == SUCCESS){
		printf("name     : %s \n", msgnew.info.name);
		printf("address  : %s \n", msgnew.info.addr);
		printf("age      : %d \n", msgnew.info.age);
		printf("level    : %d \n", msgnew.info.level);
		printf("no       : %d \n", msgnew.info.no);
		printf("phone    : %s \n", msgnew.info.phone);
		printf("type     : %d \n", msgnew.info.type);
		printf("salary   : %lf\n", msgnew.info.salary);
	}else if(msgnew.sign == FAILED){
		printf("find info failed!\n");
	}else{
		printf("somthing error!\n");
	}
	return 0;
}












